#ifndef __COMM_HPP
#define __COMM_HPP

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <string>
#include "log.hpp"


using namespace std;

const string pathname = "/home/ys";//自定义
const int proj_id = 1234;//自定义
//有概率冲突，冲突了随便更改就行

#define SIZE 1024

Log log;//创建日志对象组件

key_t GetKey()//获取key
{
    key_t x = ftok(pathname.c_str(),proj_id);
    if(x == -1)
    {
        log(Fatal,"ftok err: %s",strerror(errno));
        exit(1);
    }
    log(Info,"ftok success,key is: %d",x);
    return x;
}

int GetShareMemHelper(int flag)
{
    key_t x = GetKey();
    int shmid = shmget(x,SIZE,flag);
    //创建了一个具备唯一标识符的、大小为1024的、全新的共享内存。

    if(shmid == -1)
    {
        log(Fatal,"create share mem err: %s",strerror(errno));
        exit(2);
    }
    log(Info,"share mem success,shmid is: %d",shmid);
    return shmid;
}


//创建与获取内存接口：
int CreateShm() { return GetShareMemHelper(IPC_CREAT|IPC_EXCL|0664); }
int GetShm() { return GetShareMemHelper(IPC_CREAT); }



#endif